package org.springblade.modules.admin.service.parts;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.entity.MaintainPlanContent;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.vo.parts.PartsExecuteVO;

/**
 * @Description
 * @Author ysq
 * @Date 2024/05/16
 **/
@Slf4j
public abstract class AbstractOrderStateStrategy implements OrderStateStrategy {

	@Override
	public boolean check(PartsExecuteVO executeParams) {
		return true;
	}

	@Override
	public Object execute(PartsExecuteVO executeParams) {
		// 状态校验
//		check(executeParams);
		// 状态流转
		Object result = handle(executeParams);
		R r = (R) result;
		if (r.isSuccess()) {
			// 状态变更后处理
			after(executeParams);
		}
		return result;
	}

	/**
	 * 默认处理方法
	 * @param executeParams 执行参数
	 * @return 任务名称
	 */
	@Override
	public Object handle(PartsExecuteVO executeParams) {
		String taskName = TaskStatusEnum.getTaskName(MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), executeParams.getTargetStatus());
		log.info("=============配件工单-状态流转-{}，时间:[{}]，参数：[{}]============", taskName, DateUtil.now(), executeParams);
		// 状态变更
		MaintainPlanContent updatePlanContent = new MaintainPlanContent();
		updatePlanContent.setId(executeParams.getPlanId());
		updatePlanContent.setStatus(executeParams.getTargetStatus());
		boolean planRes = maintainPlanContentService.updateById(updatePlanContent);
		if (!planRes) {
			log.error("状态流转失败, 工单id：[{}]", executeParams.getPlanId());
			return R.status(false);
		}
		return R.data(taskName);
	}

	@Override
	public void after(PartsExecuteVO executeParams) {

	}
}
